package fina2.dcs.fis;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Remove;
import javax.ejb.Stateful;
import javax.naming.InitialContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.jboss.annotation.ejb.LocalBinding;
import org.jboss.seam.ScopeType;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.Destroy;
import org.jboss.seam.annotations.Logger;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.annotations.Scope;
import org.jboss.seam.log.Log;

import fina2.dcs.context.ContextHelper;
import fina2.dcs.i18n.LanguageSession;
import fina2.dcs.system.SysString;
import fina2.dcs.system.SysStringLocal;
import fina2.dcs.system.SysStringPk;

@Stateful
@Scope(ScopeType.SESSION)
@Name("bankManagementSession")
@LocalBinding(jndiBinding = "BankManagementSession/local")
@SuppressWarnings("unchecked")
@AutoCreate
public class BankManagementSession implements BankManagementLocal,Serializable {
	@PersistenceContext
	private EntityManager em;

	private SysStringLocal sysStringLocal = null;
	private ManagingBodyLocal manBodyLocal = null;
	private InitialContext ic = null;
	
	@Logger
	private Log log;

	public void init() {
		try {
			ic = ContextHelper.getInitialContext();
			sysStringLocal = ContextHelper.injectSysStringLocal(ic);
			manBodyLocal = ContextHelper.injectManagingBodyLocal(ic);
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
	}

	public List<BankManagement> loadManagementByBankId(BigDecimal bankId) {

		List<BankManagement> bms = new ArrayList<BankManagement>();
		try {
			ic = ContextHelper.getInitialContext();
			sysStringLocal = ContextHelper.injectSysStringLocal(ic);
			manBodyLocal = ContextHelper.injectManagingBodyLocal(ic);
			bms = em.createQuery("FROM " + BankManagement.class.getName() + " bm WHERE bm.bankId=:bankId").setParameter("bankId", bankId).getResultList();
			if (bms != null) {
				for (int i = 0; i < bms.size(); i++) {
					String name = null;
					String lastName = null;
					String post = null;

					SysString sName = sysStringLocal.findByPk(new SysStringPk(bms.get(i).getNameStrId(), LanguageSession.langId));
					SysString sLastName = sysStringLocal.findByPk(new SysStringPk(bms.get(i).getLastNameStrId(), LanguageSession.langId));

					//SysString sPost = sysStringLocal.findByPk(new SysStringPk(bms.get(i).getPostStrId(),LanguageSession.langId));
					ManagingBody mb=manBodyLocal.findById(bms.get(i).getManagingBodyId());

					name = (sName != null) ? sName.getValue() : null;
					lastName = (sLastName != null) ? sLastName.getValue() : null;
					post = (mb != null) ? mb.getPost() : null;

					bms.get(i).setName(name);
					bms.get(i).setLastName(lastName);
					bms.get(i).setPost(post);

				}
			}
		} catch (Exception ex) {
			log.error(ex.getMessage(), ex);
		}
		return bms;
	}

	@Remove
	@Destroy
	public void remove() {

	}

}
